#coding=utf-8
import numpy as np
def loadmodel(obj):
 # 读取材质
 fm=open(obj.replace('obj','mtl'),'r',encoding='UTF-8')
 ls=fm.readlines()
 fm.close()
 # 读取几何
 index=0
 mtl={}
 name=''
 for line in ls:
  if line[:6]=='newmtl':
   index+=1
   name=line.split()[1]
   mtl[name]={}
   mtl[name]['index']=index-1
   continue
  if index>0:
   item=line.split()
   if len(item)>1:
    mtl[name][item[0]]=[float(i) for i in item[1:]]
 print(mtl)
 # 材质
 mMtl=np.zeros((3*len(mtl),4),dtype=np.float32)
 for i in range(len(mtl)-1,-1,-1):
  m=mtl.popitem()[1]
  print(m)
  assert i==m['index']
  mMtl[i*3,0:3]=m['diff']
  mMtl[i*3,3:4]=m['rough']
  mMtl[i*3+1,0:3]=m['spec']
  mMtl[i*3+1,3:4]=m['ior']
  mMtl[i*3+2,0:3]=m['emit']  
 print(mMtl)
loadmodel('dragon.obj')